#include <iostream>

#include <gtest/gtest.h>

#include "melon/client/thrift_client_pool.h"
#include "SessionService.h"

using imserver::SessionServiceClient;
using imserver::SessionList;
using imserver::SessionType;
using melon::client::ClientPool;

TEST(SessionService, basic) {
  ClientPool<SessionServiceClient> pool;
  
  SessionServiceClient * p = NULL;
  SessionList res;

  for (int i = 0; i < 1; ++i) {
  // for (int i = 0; i < 1; ++i) {
    // p = pool.Alloc("10.121.215.88:9199");
    p = pool.Alloc("10.16.15.197:9199");
    if (!p) {
      std::cout << "connect error!" << std::endl;
      continue;
    }

    bool success = true;
    try {
      p->AddSession(10001, "session10001");
      p->AddSession(10001, "session10002");
      p->AddSession(10001, "session10003");
      p->AddSession(10001, "session10004");
      p->AddSession(10001, "session10005");
      p->GetSessionList(res, 10001);
      EXPECT_EQ(res.size(), 5);

      p->AddSession(10003, "session10003");
      p->AddSession(10005, "session10005");
      p->AddSession(10007, "session10005");
      std::vector<int64_t> qids;
      qids.push_back(10001);
      qids.push_back(10002);
      qids.push_back(10003);
      qids.push_back(10004);
      qids.push_back(10005);
      qids.push_back(10006);
      qids.push_back(10007);
      qids.push_back(10008);
      std::map<int64_t, SessionType::type> online_map;
      p->GetOnlineUsers(online_map, qids);
      EXPECT_EQ(online_map.size(), 4);

      p->RemoveSession(10001, "session10001");
      p->GetSessionList(res, 10001);
      EXPECT_EQ(res.size(), 4);

      p->RemoveSession(10001, "session10002");
      p->GetSessionList(res, 10001);
      EXPECT_EQ(res.size(), 3);

      online_map.clear();
      p->GetOnlineUsers(online_map, qids);
      EXPECT_EQ(online_map.size(), 4);

      p->RemoveSession(10001, "session10003");
      p->GetSessionList(res, 10001);
      EXPECT_EQ(res.size(), 2);

      p->RemoveSession(10001, "session10004");
      p->GetSessionList(res, 10001);
      EXPECT_EQ(res.size(), 1);

      online_map.clear();
      p->GetOnlineUsers(online_map, qids);
      EXPECT_EQ(online_map.size(), 4);

      p->RemoveSession(10001, "session10005");
      p->GetSessionList(res, 10001);
      EXPECT_EQ(res.size(), 0);

      online_map.clear();
      p->GetOnlineUsers(online_map, qids);
      EXPECT_EQ(online_map.size(), 3);

      p->RemoveSession(10001, "session10001");
      p->GetSessionList(res, 10001);
      EXPECT_EQ(res.size(), 0);
       
      p->RemoveSession(10005, "session10005");

      online_map.clear();
      p->GetOnlineUsers(online_map, qids);
      EXPECT_EQ(online_map.size(), 2);

      EXPECT_TRUE(online_map.find(10003) != online_map.end());
      EXPECT_TRUE(online_map.find(10007) != online_map.end());

    } catch (const std::exception & e) {
      std::cout << "error:" << e.what() << std::endl;
      success = false;
    }
    pool.Release("10.121.215.88:9199", p, success);
  }
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

